package com.stnet.web.enterprice.chinamobile;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.modules.commons.PageAction;
import com.opensymphony.xwork2.ActionContext;
import com.stnet.bases.Constants;
import com.stnet.entity.Authorities;
import com.stnet.entity.Saleorder;
import com.stnet.entity.Users;

public class SaleorderAction extends PageAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Saleorder> saleorderList;
	private Saleorder saleorder;
	private Long id;

	private String mobile;
	private String phonePassword;
	/**
	 * 用于提交update表单后，返回编辑前的页面
	 */
	private String _pageNo;

	private List<Authorities> authoritiesList;

	/**
	 * ajax请求之后的折扣价
	 * */
	private String amount;

	public SaleorderAction() {
		saleorder = new Saleorder();
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String execute() throws Exception {
		
		super.pager = saleorderManagerDao.findSaleorder(pageSize, pageNo, "-1");
		
		saleorderList = super.pager.getResultList();
		
		return Constants.SUCCESS_KEY;
	}

	/**
	 * 查看一条友情链接信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String view() throws Exception {
		setSaleorder(saleorderManagerDao.getSaleorder(id));
		return Constants.VIEW_KEY;
	}

	/**
	 * 查看一条友情链接信息，并跳转到添加页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		setSaleorder(saleorderManagerDao.getSaleorder(id));
		this.authoritiesList = this.authoritiesManagerDao.findAuthorities("2","1");
		return Constants.EDIT_KEY;
	}

	/**
	 * 修改一条友情链接信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		
		Users users = null;
		Map session = (Map) ActionContext.getContext().getSession();
		if (session.get("users") != null)
			users = (Users) session.get("users");
		Saleorder sale = this.saleorderManagerDao.getSaleorder(saleorder.getId());
		saleorder.setEditetime(sale.getEditetime());
		saleorder.setPhonePassword(phonePassword);
		saleorder.setState("1");
		saleorder.setDiscount(Long.valueOf(this.amount));
		saleorder.setSaleNo(mobile);
		
		saleorder.setUser(sale.getUser());
		
		saleorder.setSaleType(this.authoritiesManagerDao.getAuthorities(
				Long.valueOf(saleorder.getSaleType())).getName());

		Users member = this.usersManagerDao.getUsersByLoginName(mobile);
		if (member != null)
			saleorder.setSUser(member.getId());
		
		saleorderManagerDao.updateSaleorder(saleorder);
		
		String _paga_No = _pageNo.equals("")||_pageNo==null?"1":_pageNo;
		super.pager = saleorderManagerDao.findSaleorder(pageSize, Integer.valueOf(_paga_No), "-1");
		saleorderList = super.pager.getResultList();
		return Constants.SUCCESS_KEY;
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add_page() throws Exception {

		this.authoritiesList = this.authoritiesManagerDao.findAuthorities("2","1");

		return Constants.ADD_PAGE_KEY;
	}

	/**
	 * 添加一条友情链接信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {

		Users users = null;
		Map session = (Map) ActionContext.getContext().getSession();
		if (session.get("users") != null)
			users = (Users) session.get("users");

		saleorder.setEditetime(new Date());
		saleorder.setPhonePassword(phonePassword);
		saleorder.setState("1");
		saleorder.setDiscount(Long.valueOf(this.amount));
		saleorder.setSaleNo(mobile);
		saleorder.setUser(users);
		saleorder.setSaleType(this.authoritiesManagerDao.getAuthorities(
				Long.valueOf(saleorder.getSaleType())).getName());

		Users member = this.usersManagerDao.getUsersByLoginName(mobile);
		if (member != null)
			saleorder.setSUser(member.getId());
		this.saleorderManagerDao.addSaleorder(saleorder);
		return this.execute();
	}

	/**
	 * 删除一条友情链接信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		saleorderManagerDao.deleteSaleorder(id);
		return this.execute();
	}

	public List<Saleorder> getSaleorderList() {
		return saleorderList;
	}

	public void setSaleorderList(List<Saleorder> saleorderList) {
		this.saleorderList = saleorderList;
	}

	public Saleorder getSaleorder() {
		return saleorder;
	}

	public void setSaleorder(Saleorder saleorder) {
		this.saleorder = saleorder;
	}

	public String getMobile() {
		return this.mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getPhonePassword() {
		return this.phonePassword;
	}

	public void setPhonePassword(String phonePassword) {
		this.phonePassword = phonePassword;
	}

	public Long getId() {
		return this.id;
	}

	public List<Authorities> getAuthoritiesList() {
		return this.authoritiesList;
	}

	public void setAuthoritiesList(List<Authorities> authoritiesList) {
		this.authoritiesList = authoritiesList;
	}

	public String getAmount() {
		return this.amount;
	}

	public void setAmount(String amount) {
		this.amount = amount;
	}

	public String get_pageNo() {
		return _pageNo;
	}

	public void set_pageNo(String no) {
		_pageNo = no;
	}

}